package com.shengqi.xinnongren.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.ui.goods.activities.ChoosePayTypeActivity;
import com.shengqi.xinnongren.work.ui.goods.adapter.OrderGoodsAdapter;
import com.shengqi.xinnongren.work.ui.home.model.CartGoodsBean;
import com.shengqi.xinnongren.work.ui.user.model.OrderDetailsBean2;
import com.shengqi.xinnongren.work.ui.user.presenter.OrderDetails2Presenter;
import com.shengqi.xinnongren.work.ui.user.view.OrderDetails2View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderDetailsActivity2 extends BaseMvpActivity<OrderDetails2View, OrderDetails2Presenter> implements OrderDetails2View {

    private static final String EXTRA_ORDER_ID = "order_id";
    private String mOrderId;

    //店铺名称
    @BindView(R.id.tv_store_name)
    TextView mTvStoreName;
    //送达时间
    @BindView(R.id.tv_arrive_time)
    TextView mTvArriveTime;
    //收货地址
    @BindView(R.id.tv_address_info1)
    TextView mTvAddressInfo1;
    @BindView(R.id.tv_address_info2)
    TextView mTvAddressInfo2;
    //订单号
    @BindView(R.id.tv_order_number)
    TextView mTvOrderNumber;
    //下单时间
    @BindView(R.id.tv_order_create_time)
    TextView mTvOrderCreateTime;
    //订单金额
    @BindView(R.id.tv_order_amount)
    TextView mTvOrderAmount;
    //支付金额
    @BindView(R.id.tv_pay_amount)
    TextView mTvPayAmount;
    //优惠券抵扣金额
    @BindView(R.id.tv_coupon_amount)
    TextView mTvCouponAmount;
    //获得积分
    @BindView(R.id.tv_gain_integral)
    TextView mTvGainIntegral;
    //支付方式
    @BindView(R.id.tv_pay_type)
    TextView mTvPayType;
    //订单备注
    @BindView(R.id.tv_order_mark)
    TextView mTvOrderMark;
    @BindView(R.id.rv_order_goods)
    RecyclerView mRvOrderGoods;

    @BindView(R.id.tv_order_cancel)
    TextView tvOrderCancel;
    @BindView(R.id.tv_order_pay)
    TextView tvOrderPay;
    @BindView(R.id.tv_order_shouhuo)
    TextView tvConfirmReceived;
    @BindView(R.id.tv_notify_send)
    TextView tvNotifySend;
    @BindView(R.id.tv_order_evaluation)
    TextView tvOrderEvaluation;
    @BindView(R.id.tv_browse_details)
    TextView tvBrowseDetails;

    @BindView(R.id.fl_pay_container)
    FrameLayout mFlPayContainer;

    private List<CartGoodsBean> mCartGoodsList = new ArrayList<>();
    private OrderGoodsAdapter mOrderGoodsAdapter;

    /**
     * @param context
     * @param orderId 订单id
     */
    public static void start(Context context, String orderId) {
        Intent intent = new Intent(context, OrderDetailsActivity2.class);
        intent.putExtra(EXTRA_ORDER_ID, orderId);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_order_details2;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            mOrderId = intent.getStringExtra(EXTRA_ORDER_ID);
        }
        if (TextUtils.isEmpty(mOrderId)) {
            ToastHelper.getInstance(this).showError("非法进入");
            finish();
        }
    }

    @OnClick(R.id.tv_pay_for_order)
    void tv_pay_for_order() {
        ChoosePayTypeActivity.start(this, mOrderId);
    }

    @Override
    protected void initData() {
        mTvStoreName.setText("新农人");
        mOrderGoodsAdapter = new OrderGoodsAdapter(this, mCartGoodsList);
        mRvOrderGoods.setAdapter(mOrderGoodsAdapter);
        mRvOrderGoods.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mPresenter.getOrderDetails(mOrderId);
    }

    @Override
    public void onGetOrderDetails(OrderDetailsBean2 detailsBean2) {
        String operation = detailsBean2.getOperation();
        if (operation != null) {
            mFlPayContainer.setVisibility(operation.contains("1") ? View.VISIBLE : View.GONE);

//          0 取消订单 1确认支付 2确认收货 3提醒发货 4待评价 5查看详情
            tvOrderCancel.setVisibility(operation.contains("0") ? View.VISIBLE : View.GONE);
            tvOrderPay.setVisibility(operation.contains("1") ? View.VISIBLE : View.GONE);
            tvConfirmReceived.setVisibility(operation.contains("2") ? View.VISIBLE : View.GONE);
            tvNotifySend.setVisibility(operation.contains("3") ? View.VISIBLE : View.GONE);
            tvOrderEvaluation.setVisibility(operation.contains("4") ? View.VISIBLE : View.GONE);
            tvBrowseDetails.setVisibility(operation.contains("5") ? View.VISIBLE : View.GONE);
        }
        mTvOrderNumber.setText(detailsBean2.getOrderNO());
        mTvOrderCreateTime.setText(detailsBean2.getAddTime());
        mTvOrderAmount.setText(detailsBean2.getOrderAmount());
        mTvPayAmount.setText(detailsBean2.getPayAmount());
        mTvCouponAmount.setText(detailsBean2.getDiscountAmount());
        mTvGainIntegral.setText(detailsBean2.getIntegral());
        mTvArriveTime.setText(detailsBean2.getDistributionDate());
        mTvAddressInfo1.setText(detailsBean2.getMobile());
        mTvPayType.setText(detailsBean2.getPayment());
        mTvOrderMark.setText(detailsBean2.getRemarks());
        mTvAddressInfo2.setText(detailsBean2.getAddress());
        mCartGoodsList.clear();
        mCartGoodsList.addAll(detailsBean2.getCommodityList());
        mOrderGoodsAdapter.notifyDataSetChanged();
    }

    @Override
    protected OrderDetails2Presenter createPresenter() {
        return new OrderDetails2Presenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
    }


}
